package thitracnghiem;

import Utils.Date;
import Utils.Time;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Odotube
 */
public class CaThi {

    String lop;
    String maMonThi;
    int lanThi;
    String ngayThi;
    String tgBatDau;
    String tgKetThuc;
    int soCauHoi;
    int id;
    static Connection conn;
    static Statement stm;

    public CaThi(String lop, String maMonThi, int lanThi, String ngayThi, String tgBatDau, String tgKetThuc, int socauhoi, int id) {
        this.lop = lop;
        this.maMonThi = maMonThi;
        this.lanThi = lanThi;
        this.ngayThi = ngayThi;
        this.tgBatDau = tgBatDau;
        this.tgKetThuc = tgKetThuc;
        this.soCauHoi = socauhoi;
        this.id = id;
    }

    public String getThoiGianKetThucThi() {
        String[] ngay = ngayThi.split("/");
        String result = "";
        result += ngay[1] + "/" + ngay[0] + "/" + ngay[2] + " ";
        String[] gio = tgKetThuc.split(":");
        
        if (Integer.parseInt(gio[0]) >= 12) {
            result += (Integer.parseInt(gio[0]) - 12) + ":" + gio[1] + " PM";
        } else {
            result += gio[0] + ":" + gio[1] + " AM";
        }
        return result;
    }

    public static void Open() throws SQLException {
        conn = DriverManager.getConnection("jdbc:odbc:ttn");
        stm = conn.createStatement();
    }

    public static void Close() throws SQLException {
        stm.close();
        conn.close();
    }

    public static boolean isDifferent(CaThi ct1, CaThi ct2) {
        if (!ct1.getLop().equals(ct2.getLop())) {
            return true;
        }
        if (!ct1.getMaMonThi().equals(ct2.getMaMonThi())) {
            return true;
        }
        if (ct1.getLanThi() != ct2.getLanThi()) {
            return true;
        }
        if (!ct1.getNgayThi().equals(ct2.getNgayThi())) {
            return true;
        }
        if (!ct1.getTGBatDau().equals(ct2.getTGKetThuc())) {
            return true;
        }
        if (!ct1.getTGKetThuc().equals(ct2.getTGKetThuc())) {
            return true;
        }
        if (ct1.getSoCauHoi() != ct2.getSoCauHoi()) {
            return true;
        }
        return false;
    }

    public static StringBuilder getViewDSCaThi() throws SQLException {

        ArrayList<CaThi> mList = CaThi.getDSCaThi();

        StringBuilder ms = new StringBuilder();
        ms.append("<table width='100%' border=2><th>Lop");

        ms.append("<th>Ma mon thi<th>Lan thi<th>Ngay thi<th>Thoi gian bat dau<th>Thoi gian ket thuc"
                + "<th>So cau hoi<th>Xoa<th>Sua");
        for (int i = 0; i < mList.size(); i++) {
            ms.append("<tr><td with='10%'valign='center'>").append(mList.get(i).getLop()).append("</td>");
            ms.append("<td with='30%'valign='center'>").append(mList.get(i).getMaMonThi()).append("</td>");
            ms.append("<td with='10%'valign='center'>").append(mList.get(i).getLanThi()).append("</td>");
            ms.append("<td with='10%'valign='center'>").append(mList.get(i).getNgayThi()).append("</td>");
            ms.append("<td with='10%'valign='center'>").append(mList.get(i).getTGBatDau()).append("</td>");
            ms.append("<td with='10%'valign='center'>").append(mList.get(i).getTGKetThuc()).append("</td>");
            ms.append("<td with='10%'valign='center'>").append(mList.get(i).getSoCauHoi()).append("</td>");
            ms.append("<td width='5%' valign='center' align='center'><a href=/ThiTracNghiem/XuLy?xoacathi=" + mList.get(i).getID() + "&yc=quanlycathi>Xoa</td>");
            ms.append("<td width='5%' valign='center' align='center'><a href=/ThiTracNghiem/XuLy?suacathi=" + mList.get(i).getID() + "&yc=quanlycathi>Sua</td>");
        }
        ms.append("</table>");
        return ms;
    }

    public boolean isValid() throws SQLException {
        Calendar cal = Calendar.getInstance();
        String s = cal.getTime().getDate() + "/" + (cal.getTime().getMonth() + 1) + "/" + (cal.getTime().getYear() + 1900);
        Date mdate1 = Date.parseDate(s);
        Date mdate2 = Date.parseDate(this.ngayThi);
        s = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
        Time mtime1 = Time.parseTime(s);
        Time mtime2 = Time.parseTime(tgBatDau);//5
        Time mtime3 = Time.parseTime(tgKetThuc);//6
        if (mdate2.compare(mdate1) == -1) {
            return false;
        }
        if (mdate2.compare(mdate1) == 0 && mtime2.compare(mtime1) == -1) {
            return false;
        }
        if (mtime2.compare(mtime3) == 0 || mtime2.compare(mtime3) == 1) {
            return false;
        }
        String sql = "select * from CaThi where ngaythi='" + this.getNgayThi();
        ResultSet rs = stm.executeQuery(sql);
        while (rs.next()) {
            if (mtime2.compare(Time.parseTime(rs.getString(6))) == -1 || mtime3.compare(Time.parseTime(rs.getString(5))) == 1) {
                return false;
            }
        }
        return true;
    }

    public static ArrayList<CaThi> getDSCaThi() throws SQLException {
        ArrayList<CaThi> mList = new ArrayList<CaThi>();
        String sql = "select * from CaThi";
        ResultSet rs = stm.executeQuery(sql);
        while (rs.next()) {
            mList.add(new CaThi(rs.getString(1), rs.getString(2), rs.getInt(3),
                    rs.getString(4), rs.getString(5), rs.getString(6), rs.getInt(7), rs.getInt(8)));
        }
        return mList;

    }

    public static int updateCaThi(int id, CaThi ct) throws SQLException {
        String sql = "update CaThi set lop='" + ct.getLop() + "', mamonthi='" + ct.getMaMonThi()
                + "', lanthi=" + ct.getLanThi() + ",ngaythi='" + ct.getNgayThi()
                + "', tgbatdau='" + ct.getTGBatDau()
                + "', tgketthuc='" + ct.getTGKetThuc()
                + "', socauhoi=" + ct.getSoCauHoi() + " where id=" + id;
        return stm.executeUpdate(sql);
    }

    public int insCaThi() throws SQLException {
        String sql = "insert into CaThi values('" + lop + "','" + maMonThi + "'," + lanThi + ",'"
                + ngayThi + "','" + tgBatDau + "','" + tgKetThuc + "'," + soCauHoi + ")";
        return stm.executeUpdate(sql);
    }

    public static int delCaThi(int mid) throws SQLException {
        String sql = "delete CaThi where CaThi.id=" + mid;
        return stm.executeUpdate(sql);
    }

    public static CaThi getCaThi(int mid) throws SQLException {
        String sql = "select * from CaThi where CaThi.id=" + mid;
        ResultSet rs = stm.executeQuery(sql);
        if (rs.next()) {
            return new CaThi(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getString(4), rs.getString(5), rs.getString(6),
                    rs.getInt(7), rs.getInt(8));
        } else {
            return null;
        }
    }

    public void setSoCauHoi(int i) {
        this.soCauHoi = i;
    }

    public int getSoCauHoi() {
        return this.soCauHoi;
    }

    public void setLop(String lop) {
        this.lop = lop;
    }

    public String getLop() {
        return this.lop;
    }

    public void setMaMonThi(String maMonThi) {
        this.maMonThi = maMonThi;
    }

    public String getMaMonThi() {
        return this.maMonThi;
    }

    public void setLanThi(int lanThi) {
        this.lanThi = lanThi;
    }

    public int getLanThi() {
        return this.lanThi;
    }

    public void setNgayThi(String ngayThi) {
        this.ngayThi = ngayThi;
    }

    public String getNgayThi() {
        return this.ngayThi;
    }

    public void setTGBatDau(String tgBatDau) {
        this.tgBatDau = tgBatDau;
    }

    public String getTGBatDau() {
        return this.tgBatDau;
    }

    public void setTGKetThuc(String tgKetThuc) {
        this.tgKetThuc = tgKetThuc;
    }

    public String getTGKetThuc() {
        return this.tgKetThuc;
    }

    public void setID(int id) {
        this.id = id;
    }

    public int getID() {
        return this.id;
    }

    public static CaThi getCaThiByLogin(String lop) throws SQLException {

        Calendar c = Calendar.getInstance();
        Date date = new Date(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH) + 1, c.get(Calendar.YEAR));
        String s = date.toString();
        System.out.println(s);
        String query = "select * from CaThi where lop='" + lop + "' and ngaythi='" + s + "'";
        Open();
        ResultSet rs = stm.executeQuery(query);
        if (rs.next()) {
            do {

                String temp1 = rs.getString("mamonthi");
                int temp2 = rs.getInt("lanthi");

                String startTime = rs.getString("tgbatdau");
                System.out.println(startTime);
                String endTime = rs.getString("tgketthuc");
                System.out.println(endTime);
                int temp3 = rs.getInt("socauhoi");
                Time currentTime = new Time(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
                if (currentTime.compare(Time.parseTime(startTime)) == 1 && currentTime.compare(Time.parseTime(endTime)) == -1) {

                    return new CaThi(lop, temp1, temp2, date.toString(), startTime, endTime, temp3, 0);
                }
            } while (rs.next());
        }
        return null;
    }

    public static void main(String[] args) {
        try {
            CaThi.Open();

            CaThi.getCaThiByLogin("D09THA1").getThoiGianKetThucThi();
            CaThi.Close();

        } catch (SQLException ex) {
            Logger.getLogger(CaThi.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }
}
